/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ecodex.restapi;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

/**
 *
 * @author jjcarbajal
 */
public class JsonConvert {    
    
    public static <T extends Object> T deserializeObject(Class<T> classInstance,  String json) throws Exception{
        try {
            
            StreamSource stream = new StreamSource(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
            JAXBContext jaxbContext = JAXBContext.newInstance(classInstance);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
            unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
            JAXBElement<T> jaxbElement = unmarshaller.unmarshal(stream, classInstance);
            
            T entity = null;
            if (!jaxbElement.isNil()) {
                entity =  jaxbElement.getValue();
            }
            else {
                throw new WebApplicationException();
            }  
            
            return entity;

        } catch (JAXBException jaxbException) {
            throw new ProcessingException(jaxbException);
        } catch (WebApplicationException ex) {
            throw new ProcessingException("");
        }        
    }    
    
    @SuppressWarnings("unchecked")
    public static <E extends Object, L extends ArrayList<E>> L deserializeObjectList(Class<E> classInstance, String json) throws Exception{
        try {
            
            StreamSource stream = new StreamSource(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
            JAXBContext jaxbContext = JAXBContext.newInstance(classInstance);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
            unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
            JAXBElement<E> jaxbElement = unmarshaller.unmarshal(stream, classInstance);
            
            L entitiesList = null;
            if (!jaxbElement.isNil()) {
                entitiesList =  (L) jaxbElement.getValue();
            }
            else {
                throw new WebApplicationException();
            }
            
            return entitiesList;

        } catch (JAXBException jaxbException) {
            throw new ProcessingException(jaxbException);
        } catch (WebApplicationException ex) {
            throw new ProcessingException("");
        }        
    }  

}
